<?php

namespace app\admin\model;

use app\common\model\BaseModel;


class FactorConfig extends BaseModel
{


    // 表名
    protected $name = 'factor_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    public function getList($where, $size = 12, $field = '*', $order = 'id desc')
    {
        $list = $this->where(function ($query) use ($where) {

        })
            ->with([])
            ->field($field)
            ->order($order)
            ->paginate($size);
        //$list->each(function ($item) {
        //});
        return $list->toArray();

    }


    /**
     * 关联因子
     * @return void
     */
    public function factor()
    {
        return $this->belongsTo('Factor', 'factor_id', 'id');
    }


    //计算因子
    public function cal($factorlist = [], $exam = [])
    {

        //获取所有的替换项目
        $replace = [];
        $factor_ids = array_column($factorlist, 'factor_id');
        $factordata = (new \app\admin\model\Factor())->where('id', 'in', $factor_ids)->select()->toArray();
//        dump($factordata);
        $factordata = array_column($factordata, null, 'id');
        $replace['allscore'] = $exam['score'];
        $replace['allcount'] = $exam['num'];
        foreach ($factorlist as $vv) {

            if (!isset($factordata[$vv['factor_id']])) {
                throw new \Exception("系统设置异常:未设置有效因子[{$vv['factor_id']}]");
            }
            if ($vv['type'] == 'exam') {

                $replace["<{$vv['subject_exam_id']}>"] = $vv['score'];
            }
            //计算当前因子分数
            if (isset($factordata[$vv['factor_id']]['score'])) {
                $factordata[$vv['factor_id']]['score'] += $vv['score'];
            } else {
                $factordata[$vv['factor_id']]['score'] = $vv['score'];
            }
            //计算当前因子题目数
            if (isset($factordata[$vv['factor_id']]['num'])) {
                $factordata[$vv['factor_id']]['num'] += 1;
            } else {
                $factordata[$vv['factor_id']]['num'] = 1;
            }
        }
        //每个因子 对应的总分 替换
        foreach ($factordata as $v4) {
            $replace["[{$v4['code']}]>"] = $vv['score'];
        }
        $maxagv = 0;




        foreach ($factordata as &$v2) {
            foreach ($replace as $k3 => $v3) {

                $v2['expression'] = str_replace($k3, $v3, $v2['expression']);
            }


//            throw new \Exception($v2['expression']);
            $v2['cal_score'] = round(eval(" return {$v2['expression']};"),2);
            $v2['avg_score'] =round( $v2['cal_score'] / $v2['num'],2);

//            dump($v2);
            //匹配报告
            $report = (new \app\admin\model\FactorConfig())->where('factor_id', $v2['id'])->where('min', "<=", $v2['cal_score'])
                ->where('max', ">", $v2['cal_score'])->find();
//            dump($report);die;
            if (empty($report)) {
                throw new \Exception("系统设置异常:未设置有效因子报告[{$v2['id']}][{$v2['cal_score']}]");
            }
            $v2['content'] = $report['content'];
            $v2['desc'] = $report['scoreintro'];
            if ($v2['avg_score'] > $maxagv) {
                $maxagv = $v2['avg_score'];
            }
        }
        //设置表的最大值
        $_factordata =[];
        foreach ($factordata as &$v5) {
            $v5['max'] = $maxagv;

            $_factordata[] = [
                'title' => $v5['name'], 'score' =>$v5['cal_score'], 'avg_score' =>$v5['avg_score'], 'max' => $maxagv, 'content'=>$v5['content'],'desc'=>$v5['desc'],'intro'=>$v5['intro']
            ];
        }
        return $_factordata;
    }


}
